fn main() {
    let nums1 = vec![1, 2];
    let nums2 = vec![3, 4];
    let res = Solution::find_median_sorted_arrays(nums1, nums2);
    println!("{}", res);
}

struct Solution;

impl Solution {
    pub fn find_median_sorted_arrays(nums1: Vec<i32>, nums2: Vec<i32>) -> f64 {
        let length = nums1.len() + nums2.len();
        let count = length / 2;
        let mut index1 = 0;
        let mut index2 = 0;
        let mut left = 0;
        let mut right = 0;
        for _i in 0..=count {
            if let Some(n1) = nums1.get(index1) {
                if let Some(n2) = nums2.get(index2) {
                    if n1 < n2 {
                        left = right;
                        right = *n1;
                        index1 = index1 + 1;
                    } else {
                        left = right;
                        right = *n2;
                        index2 = index2 + 1;
                    }
                } else {
                    left = right;
                    right = *n1;
                    index1 = index1 + 1;
                }
            } else {
                if let Some(n2) = nums2.get(index2) {
                    left = right;
                    right = *n2;
                    index2 = index2 + 1;
                } else {
                    break;
                }
            }
        }
        if length % 2 == 0 {
            // 4 => (1, 2)
            (left + right) as f64 / 2.0
        } else {
            // 7 => 3
            right as f64
        }
    }
}
